package com.siderakis.orgunit.client;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.siderakis.orgunit.client.domain.OrgUnit;
import com.siderakis.orgunit.client.ui.OrgUnitAccessSettings;

public class Admin implements EntryPoint {

  private native final JavaScriptObject getOrgUnits()/*-{
		return $wnd.org.orgUnits;
  }-*/;

  private native final JavaScriptObject getOrgName()/*-{
		return $wnd.org.orgName;
  }-*/;

  public void onModuleLoad() {

    JavaScriptObject json = getOrgUnits();
    JsArray<OrgUnit> unitsListFromJson = json.cast();

    final OrgUnit orgName = getOrgName().cast();
    final List<OrgUnit> list = new ArrayList<OrgUnit>();
    for (int i = 0; i < unitsListFromJson.length(); i++) {
      list.add(unitsListFromJson.get(i));
    }

    OrgUnitAccessSettings accessSettings = new OrgUnitAccessSettings(list, orgName);
    RootLayoutPanel.get().add(accessSettings);
  }

}
